<?php
session_name("interoprouter");
session_start();

/*
 * CONFIGURACAO OBRIGATORIAS
 * variaveis de configuracao obrigatorias para funcionamento do site
 */
$name = "projetos";	// nome da pagina atual
$_SESSION["page"] = "projetos.php";
if ( isset( $_SESSION["lang"] ) == false ) $_SESSION["lang"] = "br";
$lang = $_SESSION["lang"];


/*
 * CONFIGURACAO 
 * variaveis de configuracao
 */
$gatelevel = "account";
$title = "Website";

/*
 * CABECALHO DA PAGINA
 * includes e inicio da construcao do layout
 */
ob_start(); include("modules/session.php"); ob_end_clean();
ob_start(); include("modules/database.php"); ob_end_clean();
include("template/templateBodyGen.php");

$javascripts = array();
makeHeader($title, $javascripts);

/*
 * CONTROLES DO CASO DE USO
 * funcoes que o caso de uso vai executar
 */
//$exemplo = array("dado 1", "dado 2", "dado 3", "dado 4");

	$accountId = $_SESSION["accountId"];
	$projectId = trim($_GET["projectId"]);

	if ($projectId == "")
	{ // user clicked on menu bar => show projects
		$flagType = "projects";
		// retrieve all projects
		$dbSql = "SELECT p.* FROM projects p WHERE p.accountId = $accountId ORDER BY p.projectTimestampCreated";
		$dbResult = @mysql_query($dbSql);
		if ($dbResult == false)
			$flagDB = false;
		else if (@mysql_num_rows($dbResult) == 0)
		{
			$flagDB = true;
			$flagResult = false;
		}
		else // BD ligado, e encontrou ao menos um projeto cadastrado
		{
			$flagDB = true;
			$flagResult = true;
			while ($dbRow = @mysql_fetch_assoc($dbResult))
			{
				$projectId = $dbRow["projectId"];
				$projectName = $dbRow["projectName"];
				$projectDate = $dbRow["projectTimestampCreated"];
				$data[] = array("projectId" => $projectId, "projectName" => $projectName, "projectDate" => $projectDate);
			}
			@mysql_free_result($dbResult);
		}
	}
	else
	{ // user selected one project on page => show jobs
		$flagType = "jobs";
		// primeiro pega o nome do projeto
		$dbSql = "SELECT p.projectName FROM projects p WHERE p.accountId = $accountId AND p.projectId = $projectId";
		$dbResult = @mysql_query($dbSql);
		$dbRow = @mysql_fetch_assoc($dbResult);
		$projectName = $dbRow["projectName"];
		@mysql_free_result($dbResult);
		
		//pega os jobs do projeto e questão
		$dbSql = "SELECT j.* FROM jobs j WHERE j.projectId=$projectId ORDER BY j.jobStatus DESC";
		$dbResult = @mysql_query($dbSql);
		if ($dbResult == false)
			$flagDB = false;
		else if (@mysql_num_rows($dbResult) == 0)
		{
			$flagDB = true;
			$flagResult = false;
		}
		else // BD ligado, e encontrou ao menos um job cadastrado
		{
			$flagDB = true;
			$flagResult = true;
			while ($dbRow = @mysql_fetch_assoc($dbResult))
			{
				$jobId = $dbRow["jobId"];
				$jobName = $dbRow["jobName"];
				$jobStatus = $dbRow["jobStatus"];
				$data[] = array("jobId" => $jobId, "jobName" => $jobName, "jobStatus" => $jobStatus);
			}
			@mysql_free_result($dbResult);
		}
	}

/*
 * RENDERIZA PAGINA
 * aqui vao os includes e as variaveis de configuracao
 */
// $flagDB # database on or off
// $flagResult # valid result or not
// $flagType # defines the type of selection
// $projectName
// $data[] # array with projects/jobs information
include("lang/{$lang}/{$name}.php");



/*
 * RODAPE DA PAGINA
 * encerra a pagina coma as tags necessarias
 */
makeFooter();

?>